package com.adam.example.aggregation.metric;

import com.adam.example.TransportClientHolder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder;

import java.net.UnknownHostException;

public class PercentilesAggregationExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        PercentilesAggregationBuilder percentilesAggregationBuilder = AggregationBuilders
                .percentiles("agg").field("price");
        SearchResponse searchResponse = transportClient.prepareSearch("test_books")
                .setSize(0).addAggregation(percentilesAggregationBuilder).get();
        Percentiles percentiles = searchResponse.getAggregations().get("agg");
        for(Percentile percentile: percentiles) {
            System.out.println(percentile.getPercent() + "%:" + percentile.getValue());
        }

        transportClient.close();
    }

}
